home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / sockdem.zip / _SETUP.1 / socketsDlg.cpp < prev    next >
C/C++ Source or Header  |  1997-06-19  |  10KB  |  384 lines

  1. // socketsDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "sockets.h"
  6. #include "socketsDlg.h"
  7.  
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAboutDlg dialog used for App About
  17.  
  18. class CAboutDlg : public CDialog
  19. {
  20. public:
  21.     CAboutDlg();
  22.  
  23. // Dialog Data
  24.     //{{AFX_DATA(CAboutDlg)
  25.     enum { IDD = IDD_ABOUTBOX };
  26.     //}}AFX_DATA
  27.  
  28.     // ClassWizard generated virtual function overrides
  29.     //{{AFX_VIRTUAL(CAboutDlg)
  30.     protected:
  31.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  32.     //}}AFX_VIRTUAL
  33.  
  34. // Implementation
  35. protected:
  36.     //{{AFX_MSG(CAboutDlg)
  37.     //}}AFX_MSG
  38.     DECLARE_MESSAGE_MAP()
  39. };
  40.  
  41. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  42. {
  43.     //{{AFX_DATA_INIT(CAboutDlg)
  44.     //}}AFX_DATA_INIT
  45. }
  46.  
  47. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  48. {
  49.     CDialog::DoDataExchange(pDX);
  50.     //{{AFX_DATA_MAP(CAboutDlg)
  51.     //}}AFX_DATA_MAP
  52. }
  53.  
  54. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  55.     //{{AFX_MSG_MAP(CAboutDlg)
  56.         // No message handlers
  57.     //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CSocketsDlg dialog
  62.  
  63. CSocketsDlg::CSocketsDlg(CWnd* pParent /*=NULL*/)
  64.     : CDialog(CSocketsDlg::IDD, pParent)
  65. {
  66.     //{{AFX_DATA_INIT(CSocketsDlg)
  67.     m_ed3 = _T("");
  68.     m_ed4 = _T("");
  69.     m_RemotePort = 0;
  70.     m_LocalPort = 0;
  71.     m_rxText = _T("");
  72.     m_txText = _T("");
  73.     m_localIP = _T("");
  74.     m_remoteIP = _T("");
  75.     m_state = 0;
  76.     //}}AFX_DATA_INIT
  77.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  78.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  79. }
  80.  
  81. void CSocketsDlg::DoDataExchange(CDataExchange* pDX)
  82. {
  83.     CDialog::DoDataExchange(pDX);
  84.     //{{AFX_DATA_MAP(CSocketsDlg)
  85.     DDX_Text(pDX, IDC_EDIT3, m_ed3);
  86.     DDX_Text(pDX, IDC_EDIT4, m_ed4);
  87.     DDX_Control(pDX, IDC_WINSOCK1, m_mysock);
  88.     DDX_Text(pDX, IDC_EDIT1, m_RemotePort);
  89.     DDV_MinMaxLong(pDX, m_RemotePort, 0, 65535);
  90.     DDX_Text(pDX, IDC_EDIT2, m_LocalPort);
  91.     DDV_MinMaxLong(pDX, m_LocalPort, 0, 65535);
  92.     DDX_Text(pDX, IDC_EdRX, m_rxText);
  93.     DDX_Text(pDX, IDC_EdTX, m_txText);
  94.     DDX_Text(pDX, IDC_EDIT5, m_localIP);
  95.     DDX_Text(pDX, IDC_EDIT6, m_remoteIP);
  96.     DDX_Text(pDX, IDC_STATE, m_state);
  97.     //}}AFX_DATA_MAP
  98. }
  99.  
  100. BEGIN_MESSAGE_MAP(CSocketsDlg, CDialog)
  101.     //{{AFX_MSG_MAP(CSocketsDlg)
  102.     ON_WM_SYSCOMMAND()
  103.     ON_WM_PAINT()
  104.     ON_WM_QUERYDRAGICON()
  105.     ON_BN_CLICKED(IDC_BUTTON1, OnSetSocket)
  106.     ON_EN_CHANGE(IDC_EdTX, OnChangeEdTX)
  107.     ON_WM_TIMER()
  108.     ON_WM_DESTROY()
  109.     ON_BN_CLICKED(IDC_Send, OnSend)
  110.     //}}AFX_MSG_MAP
  111. END_MESSAGE_MAP()
  112.  
  113. /////////////////////////////////////////////////////////////////////////////
  114. // CSocketsDlg message handlers
  115.  
  116. BOOL CSocketsDlg::OnInitDialog()
  117. {
  118.     CDialog::OnInitDialog();
  119.  
  120.     // Add "About..." menu item to system menu.
  121.  
  122.     // IDM_ABOUTBOX must be in the system command range.
  123.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  124.     ASSERT(IDM_ABOUTBOX < 0xF000);
  125.  
  126.     pMySocket =(CMSWinsockControl*) GetDlgItem (IDC_WINSOCK1);    
  127.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  128.     if (pSysMenu != NULL)
  129.     {
  130.         CString strAboutMenu;
  131.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  132.         if (!strAboutMenu.IsEmpty())
  133.         {
  134.             pSysMenu->AppendMenu(MF_SEPARATOR);
  135.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  136.         }
  137.     }
  138.  
  139.     // Set the icon for this dialog.  The framework does this automatically
  140.     //  when the application's main window is not a dialog
  141.     SetIcon(m_hIcon, TRUE);            // Set big icon
  142.     SetIcon(m_hIcon, FALSE);        // Set small icon
  143.     
  144.     // TODO: Add extra initialization here
  145.     pMySocket->SetProtocol( (long) 1);    
  146.     m_ed4=pMySocket->GetLocalHostName();
  147.     m_state=pMySocket->GetState();
  148.     VariantInit(&myVar);
  149.     myVar.bstrVal=SysAllocString(L"Hello world.");
  150.         
  151.     //start the timer which updaes the STATE field.
  152.     myTimer=SetTimer(1,100,NULL);
  153.  
  154.     UpdateData(FALSE);
  155.     return TRUE;  // return TRUE  unless you set the focus to a control
  156. }
  157.  
  158. void CSocketsDlg::OnSysCommand(UINT nID, LPARAM lParam)
  159. {
  160.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  161.     {
  162.         CAboutDlg dlgAbout;
  163.         dlgAbout.DoModal();
  164.     }
  165.     else
  166.     {
  167.         CDialog::OnSysCommand(nID, lParam);
  168.     }
  169. }
  170.  
  171. // If you add a minimize button to your dialog, you will need the code below
  172. //  to draw the icon.  For MFC applications using the document/view model,
  173. //  this is automatically done for you by the framework.
  174.  
  175. void CSocketsDlg::OnPaint() 
  176. {
  177.     if (IsIconic())
  178.     {
  179.         CPaintDC dc(this); // device context for painting
  180.  
  181.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  182.  
  183.         // Center icon in client rectangle
  184.         int cxIcon = GetSystemMetrics(SM_CXICON);
  185.         int cyIcon = GetSystemMetrics(SM_CYICON);
  186.         CRect rect;
  187.         GetClientRect(&rect);
  188.         int x = (rect.Width() - cxIcon + 1) / 2;
  189.         int y = (rect.Height() - cyIcon + 1) / 2;
  190.  
  191.         // Draw the icon
  192.         dc.DrawIcon(x, y, m_hIcon);
  193.     }
  194.     else
  195.     {
  196.         CDialog::OnPaint();
  197.     }
  198. }
  199.  
  200. // The system calls this to obtain the cursor to display while the user drags
  201. //  the minimized window.
  202. HCURSOR CSocketsDlg::OnQueryDragIcon()
  203. {
  204.     return (HCURSOR) m_hIcon;
  205. }
  206.  
  207.  
  208. //Here is the code to set the SOCKET properties !
  209. void CSocketsDlg::OnSetSocket() 
  210. {
  211.     //Get data from form INTO member vars.
  212.     UpdateData(TRUE);
  213.  
  214.     //Set REMOTE host by name
  215.     pMySocket->SetRemoteHost(m_ed3);
  216.  
  217.     //Set REMOTE port from Long member variable.
  218.     pMySocket->SetRemotePort(m_RemotePort);
  219.     
  220.     //Set LOCAL port from Long member variable    
  221.     pMySocket->SetLocalPort(m_LocalPort);
  222.  
  223.  
  224.     //get IP addresses 
  225.     m_remoteIP =pMySocket->GetRemoteHostIP();
  226.     m_localIP =pMySocket->GetLocalIP();
  227.     m_RemotePort=pMySocket->GetRemotePort();
  228.     m_LocalPort=pMySocket->GetLocalPort();
  229.     m_ed4=pMySocket->GetLocalHostName();
  230.  
  231.     //Set data from member varianles onto form.
  232.     UpdateData(FALSE);
  233.     
  234.     //attempt a send.
  235.     myVar.vt=VT_BSTR;
  236.     myVar.bstrVal=SysAllocString(L"Hello world.");
  237.     pMySocket->SendData(myVar);
  238.     
  239.  
  240.     //Set data from member varianles onto form.
  241.     UpdateData(FALSE);
  242. }
  243.  
  244. void CSocketsDlg::OnChangeEdTX() 
  245. {
  246.     // TODO: If this is a RICHEDIT control, the control will not
  247.     // send this notification unless you override the CDialog::OnInitDialog()
  248.     // function to send the EM_SETEVENTMASK message to the control
  249.     // with the ENM_CHANGE flag ORed into the lParam mask.
  250.     
  251.     // TODO: Add your control notification handler code here
  252.     
  253. }
  254.  
  255. BEGIN_EVENTSINK_MAP(CSocketsDlg, CDialog)
  256.     //{{AFX_EVENTSINK_MAP(CSocketsDlg)
  257.     ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 0 /* DataArrival */, OnDataArrivalWinsock1, VTS_I4)
  258.     ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 6 /* Error */, OnErrorWinsock1, VTS_I2 VTS_PBSTR VTS_I4 VTS_BSTR VTS_BSTR VTS_I4 VTS_PBOOL)
  259.     ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 1 /* Connect */, OnConnectWinsock1, VTS_NONE)
  260.     ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 2 /* ConnectionRequest */, OnConnectionRequestWinsock1, VTS_I4)
  261.     ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 5 /* Close */, OnCloseWinsock1, VTS_NONE)
  262.     ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 3 /* SendProgress */, OnSendProgressWinsock1, VTS_I4 VTS_I4)
  263.     ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 4 /* SendComplete */, OnSendCompleteWinsock1, VTS_NONE)
  264.     //}}AFX_EVENTSINK_MAP
  265. END_EVENTSINK_MAP()
  266.  
  267. void CSocketsDlg::OnDataArrivalWinsock1(long bytesTotal) 
  268. {
  269.     // TODO: Add your control notification handler code here
  270.     char* myString2="These bytes arrived 9999999999999999999999";
  271.     VARIANT myData;
  272.     VariantInit(&myData);
  273.  
  274.  
  275.     VARIANT myTypeOfVar;
  276.     VariantInit(&myTypeOfVar);
  277.  
  278.     myTypeOfVar.vt=VT_I2;
  279.     myTypeOfVar.iVal=VT_BSTR;
  280.  
  281.     VARIANT myMaxLen;
  282.     VariantInit(&myMaxLen);
  283.  
  284.     myMaxLen.vt=VT_I2;
  285.     myMaxLen.iVal=999;
  286.         
  287.     sprintf(myString2,"These bytes arrived %d ",bytesTotal);
  288.     
  289.     myData.vt = 8;
  290.  
  291.     pMySocket->GetData( &myData, myTypeOfVar , myMaxLen );
  292.  
  293.     m_txText = m_txText + V_BSTR(&myData);
  294.     UpdateData(FALSE);
  295.     
  296.  
  297. }
  298.  
  299. void CSocketsDlg::OnErrorWinsock1(short Number, BSTR FAR* Description, long Scode, LPCTSTR Source, LPCTSTR HelpFile, long HelpContext, BOOL FAR* CancelDisplay) 
  300. {
  301.     // TODO: Add your control notification handler code here
  302.     MessageBox("An error follows:-");
  303.     MessageBox((const char *)Description);
  304. }
  305.  
  306. void CSocketsDlg::OnConnectWinsock1() 
  307. {
  308.     // TODO: Add your control notification handler code here
  309.     MessageBox("OnConnect");    
  310. }
  311.  
  312. void CSocketsDlg::OnConnectionRequestWinsock1(long requestID) 
  313. {
  314.     // TODO: Add your control notification handler code here
  315.     MessageBox("OnConnectionRequest");    
  316.     
  317. }
  318.  
  319. void CSocketsDlg::OnCloseWinsock1() 
  320. {
  321.     // TODO: Add your control notification handler code here
  322.     MessageBox("OnClose");    
  323.     
  324. }
  325.  
  326. void CSocketsDlg::OnSendProgressWinsock1(long bytesSent, long bytesRemaining) 
  327. {
  328.     // TODO: Add your control notification handler code here
  329.     MessageBox("OnSendProgress");    
  330.     
  331. }
  332.  
  333. void CSocketsDlg::OnSendCompleteWinsock1() 
  334. {
  335.     // TODO: Add your control notification handler code here
  336.     MessageBox("OnSendComplete");    
  337. }
  338.  
  339. void CSocketsDlg::OnTimer(UINT nIDEvent) 
  340. {
  341.     // TODO: Add your message handler code here and/or call default
  342.     UpdateData(TRUE);
  343.         //get port state
  344.         m_state=pMySocket->GetState();
  345.         //get IP addresses and stuff
  346.         m_remoteIP =pMySocket->GetRemoteHostIP();
  347.         m_localIP =pMySocket->GetLocalIP();        
  348.         m_ed4=pMySocket->GetLocalHostName();
  349.  
  350.     UpdateData(FALSE);
  351.     
  352.     CDialog::OnTimer(nIDEvent);
  353. }
  354.  
  355. void CSocketsDlg::OnDestroy() 
  356. {
  357.     KillTimer(myTimer);    
  358.     pMySocket->Close();
  359.  
  360.     CDialog::OnDestroy();
  361.     
  362.     
  363.     
  364.     
  365. }
  366.  
  367. void CSocketsDlg::OnSend() 
  368. {
  369.     // TODO: Add your control notification handler code here
  370.  
  371.     //attempt to send some data if the port is open.
  372.     if(pMySocket->GetState() == 1)
  373.     {
  374.         myVar.vt=VT_BSTR;
  375.         myVar.bstrVal=SysAllocString(L"Hello world.");
  376.         pMySocket->SendData(myVar);
  377.     }
  378.     else 
  379.     {
  380.         MessageBox("Cannot send, port is not open.");
  381.     }
  382.     
  383. }
  384.